package web.Order;

import Dao.MemberDao;
import entity.CourtsBean;
import entity.MemberBean;
import entity.OrdersBean;
import services.CourtsServiceAll;
import services.MemberServices;
import services.OrdersService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/addOrders")
public class AddOrders extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json");
        int courtId = Integer.parseInt(req.getParameter("court_id"));
        String uuid = req.getParameter("UUID");
        CourtsBean courtsByCourId = new CourtsServiceAll().getCourtsByCourId(courtId);
        OrdersBean ordersBean = new OrdersBean();
        ordersBean.setOrder_No(uuid);
        ordersBean.setAvailable_time(courtsByCourId.getCourts_end_time());
        ordersBean.setOrder_time(courtsByCourId.getCourts_start_time());
        ordersBean.setCourts_table(courtsByCourId.getCourts_table());
        ordersBean.setCourt_id(courtsByCourId.getCourt_id());
        ordersBean.setCourts_type_name(courtsByCourId.getCourts_type_name());
        // 需要判断是否为会员结算（可以根据请求是否传过来电话号码没有传电话号码就是游客）
        String PhoneNum = req.getParameter("PhoneNum");
        if("".equals(PhoneNum)){
            //游客操作
            ordersBean.setMember_id(1);
        }else{
            //会员操作
            MemberBean memberByPhone = new MemberServices().getMemberByPhone(PhoneNum);
            ordersBean.setMember_id(memberByPhone.getMember_id());
        }
        ordersBean.setPayment_state("未支付");
        ordersBean.setTotal_amount(0);
        int num = 0;
        try {
            num = new OrdersService().addOrders(ordersBean);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        resp.getWriter().println(num > 0?"新增成功":"新增失败");
    }
}
